import store from "@/store";

const tokens = {
  admin: {
    token: "admin-token",
  },
  editor: {
    token: "editor-token",
  },
};

const users = {
  "admin-token": {
    user: {
      roles: ["admin"],
      introduction: "I am a super administrator",
      avatar:
        "https://charmingcheng20.oss-cn-zhangjiakou.aliyuncs.com/wx-api/20201204/613f4398e74efd9f7585be8b0b890e13.png",
      username: "Super Admin",
    },
    system: {},
  },
  "editor-token": {
    user: {
      roles: ["editor"],
      introduction: "I am an editor",
      avatar:
        "https://charmingcheng20.oss-cn-zhangjiakou.aliyuncs.com/wx-api/20201204/613f4398e74efd9f7585be8b0b890e13.png",
      username: "Normal Editor",
    },
    system: {},
  },
};

export default [
  // user login
  {
    url: "/admin/login",
    type: "post",
    response: (config) => {
      const { username } = config.body;
      const token = tokens[username];

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: "账号或密码不正确",
        };
      }

      return {
        code: 20000,
        data: token,
      };
    },
  },

  // get user info
  {
    url: "/admin/userinfo",
    type: "get",
    response: (config) => {
      const token = store.getters.token;
      const info = users[token];

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: "获取用户信息失败",
        };
      }

      return {
        code: 20000,
        data: info,
      };
    },
  },

  // user logout
  {
    url: "/admin/logout",
    type: "post",
    response: (_) => {
      return {
        code: 20000,
        data: "success",
      };
    },
  },
];
